//
//  VisionBeautiyController.m
//  BuffetNews
//
//  Created by weidawei on 14/11/9.
//  Copyright (c) 2014年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "VisionBeautiyController.h"
#import "visionController.h"
#import "SecondViewController.h"
@interface VisionBeautiyController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, retain) UITableView *table;
@end

@implementation VisionBeautiyController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
- (void)dealloc
{
    [_table release];
    [_sonsArr release];
    [super dealloc];
}

- (void)startData
{
    self.table = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 110) style:UITableViewStylePlain];
    self.table.delegate = self;
    self.table.dataSource = self;
    [self.view addSubview:self.table];
    self.table.alpha = 0.9;
    
    UIImageView *backgrnd=[[UIImageView alloc] initWithFrame:CGRectMake(0, -50, 320, 568)];
    backgrnd.contentMode=UIViewContentModeScaleAspectFit;
    
    backgrnd.image=[UIImage imageNamed:@"buff1.png"];
    [self.view addSubview:self.table];
    [self.view addSubview:backgrnd];
    [self.view bringSubviewToFront:self.table];
    
    
    
    [_table release];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [self.sonsArr count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSMutableArray *arr = [self.sonsArr[section] objectForKey:@"sons"];
    return [arr count];
}


- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    NSString *string = [self.sonsArr[section] objectForKey:@"block_title"];
    return string;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"1"];
    if (!cell) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"1"]autorelease];
    }
    
    NSMutableArray *arr = [self.sonsArr[indexPath.section] objectForKey:@"sons"];
    NSString *string = [arr[indexPath.row] objectForKey:@"block_title"];
    
    cell.textLabel.text = string;
    cell.textLabel.font = [UIFont systemFontOfSize:15];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{    
    NSDictionary *dictionary = self.sonsArr[indexPath.section];
    NSMutableArray *arr = [dictionary objectForKey:@"sons"];
    NSString *photo = [arr[indexPath.row] objectForKey:@"data_type"];
    if ([photo isEqualToString:@"photo"]) {
        visionController *visionVC = [[visionController alloc] init];
        visionVC.visionApi = [arr[indexPath.row] objectForKey:@"api_url"];
        [visionVC setModalTransitionStyle:UINavigationControllerHideShowBarDuration];
        [self presentViewController:visionVC animated:YES completion:^{
            
        }];
        [visionVC release];
    } else {
        SecondViewController *secondVC = [[SecondViewController alloc] init];
        secondVC.apiString = [arr[indexPath.row] objectForKey:@"api_url"];
        
        [secondVC setModalTransitionStyle:UINavigationControllerHideShowBarDuration];

        [self presentViewController:secondVC animated:YES completion:^{
            
        }];
        [secondVC release];
    }
}

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    //1. Setup the CATransform3D structure
    CATransform3D translation;
    // rotation = CATransform3DMakeRotation( (90.0*M_PI)/180, 0.0, 0.7, 0.4);
    translation = CATransform3DMakeTranslation(0, 480, 0);
    //rotation.m34 = 1.0/ -600;
    
    
    //2. Define the initial state (Before the animation)
    cell.layer.shadowColor = [[UIColor blackColor]CGColor];
    cell.layer.shadowOffset = CGSizeMake(10, 10);
    cell.alpha = 0;
    
    cell.layer.transform = translation;
    cell.layer.anchorPoint = CGPointMake(0, 0.5);
    
    //!!!FIX for issue #1 Cell position wrong------------
    if(cell.layer.position.x != 0){
        cell.layer.position = CGPointMake(0, cell.layer.position.y);
    }
    
    //4. Define the final state (After the animation) and commit the animation
    
    [UIView beginAnimations:@"translation" context:NULL];
    [UIView setAnimationDuration:0.8];
    cell.layer.transform = CATransform3DIdentity;
    
    cell.alpha = 1;
    cell.layer.shadowOffset = CGSizeMake(0, 0);
    
    [UIView commitAnimations];
}




- (void)viewDidLoad
{
    [super viewDidLoad];
    [self startData];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
